<?php
/**
 * 表格页面生成类
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/23
 * Time: 9:50
 */

namespace App\Extend\Admin\Common\Html;


class Table
{
    private $search = ''; //搜索
    private $table = ''; //表格
    private $page = ''; //表格分页
    private $tableTitle = ''; //表格标题
    private $tab = ''; //选项卡
    private $delBtn = ''; //删除按钮

    public function __construct() {

    }

    //添加按钮
    public function addTitle($title, $dataType = '') {
        // 默认添加按钮
        if(is_string($title)) {
            return $this->_addTitle($title);
        }

        // 指定标签的按钮
        if($dataType) {
            return $this->_addTitle($title, $dataType);
        }

        // 批量数组的按钮
        if(is_array($title)) {
            $button = '';
            foreach($title as $dataType => $item) {
                $button .= '<button class="layui-btn" data-type="'. $dataType .'">'. $item .'</button>' ."\r\n";
            }
            $html = '<div class="demoTable" style="margin-bottom: 20px;">
                    '. $button .'
                </div>';
            return $html;
        }
    }

    private function _addTitle($title, $dataType='add') {
        return '<div class="demoTable" style="margin-bottom: 20px;">
                    <button class="layui-btn" data-type="'. $dataType .'">新增'. $title .'</button>
                </div>';
    }

    public function setTitle($title,$classname = ''){
        return '<div class="demoTable" style="margin-bottom: 20px;">
                    <button class="layui-btn '.$classname.'" data-type="'.$classname.'">'. $title .'</button>
                </div>';
    }

    //内容页
    public function bodyBlock() {
        $html = '<div class="body_block">';
        if($this->tableTitle) $html .= $this->tableTitle;
        if($this->tab) $html .= $this->tab;
        if($this->search) $html .= $this->search;
        if($this->table) $html .= $this->table;
        if($this->page) $html .= $this->page;
        if($this->delBtn) $html .= $this->delBtn;
        $html .= '</div>';
        return $html;
    }

    //表格title
    public function tableTitle($title) {
        $this->tableTitle = '<div class="layui-card-header title_big">'.$title.'列表<!--设备列表--></div>'. "\r\n";
        return $this;
    }

    //表格选项卡
    public function tableTab($li) {
        $tab = '<ul class="layui-tab-title">' . "\r\n";

            $html_li = '';
            $class = '';
            foreach($li as $k=>$v) {
                if($k == 0) $class = 'layui-this';
                else $class = '';
                $html_li .= '<li lay-id="' . $k . '" class="'. $class .'">'.$v.'</li>';
            }
            $tab .= $html_li . "\r\n";

        $tab .= '</ul>';
        $this->tab = $tab;
        return $this;
    }

    //搜索框
    public function search($search, $get=[], $ext_button=[]) {
        include_once '../application/common/view/public/search.php';
        $search = search($search,$get,$ext_button);
        $this->search = '<div class="layui-card-header title_big layui-form">'. $search .'</div>' . "\r\n";
        return $this;
    }

    //table主体
    public function table($id='test') {
        $this->table = '<table id="'.$id.'" lay-filter="'.$id.'"></table>' . "\r\n";
        $this->page = '<div style="" id="page"></div>' . "\r\n";
        return $this;
    }

    //删除按钮
    public function delBtn($data_type='del') {
        $html = '<div style="position: relative;">
            <button id="btn-del" class="layui-btn layui-btn-xs" data-type="'.$data_type.'">删除</button>
        </div>';
        $this->delBtn = $html;
        return $this;
    }

    //表格内操作按钮
    public function jsBtn($id , $data) {
        $html = '<script type="text/html" id="'.$id.'">'."\r\n";
        foreach($data as $v) {
            $pre = empty($v[2]['pre']) ? '' : '{{# ' . $v[2]['pre'] . ' }}';
            $pre .= "\r\n";
            $next = empty($v[2]['next']) ? '' : '{{# ' . $v[2]['next'] . ' }}';
            $next .= "\r\n";
            $class = empty($v[2]['class']) ? '' : $v[2]['class'];
            $html .= $pre. '<a class="layui-btn ' . $class . ' layui-btn-xs" lay-event="'. $v[0] .'">'.$v[1].'</a>' ."\r\n". $next;
        }
        $html .= '</script>';
        return $html;
    }

    //表格内开关
    public function toolCheckbox($id , $field,$key='id',$text='启用|禁用') {
        $html = '<script type="text/html" id="'. $id .'">';
        $html .= '{{#  if(d.'. $field .' == 1){ }}';
        $html .= '<input type="checkbox" name="{{d.'.$key.'}}" lay-skin="switch" lay-filter="'.$id.'" lay-text="'.$text.'" checked>';
        $html .= '{{#  } else { }}';
        $html .= '<input type="checkbox" name="{{d.'.$key.'}}" lay-skin="switch" lay-filter="'.$id.'" lay-text="'.$text.'">';
        $html .= '{{# } }}';
        $html .= '</script>';
        return $html;
    }

    //获取js表格重载所需搜索条件
    public function getSearchJsKey($search, $scope='', $key='key') {
        $js = '';
        foreach($search as $v) {
             $js .= $key. '.'.$v['key'].' = $("'. $scope .' #search_'.$v['key'].'").val();'."\r\n";
        }
        return $js;
    }

    //获取js表格重载所需搜索条件,url格式字符串
    public function getSearchJsUrl($search) {
        $js = '';
        foreach($search as $v) {
            $js .= 'url += "'.$v['key'].'" + "=" + $("#search_'.$v['key'].'").val() + "&";'."\r\n";
        }
        return $js;
    }

    //获取切换卡
    public function getTab() {
        $tab = $this->tab;
        $this->tab = '';
        return $tab;
    }

    //获取搜索
    public function getSearch() {
        $search = $this->search;
        $this->search = '';
        return $search;
    }

    //获取表格
    public function getTable() {
        return $this->table ."\r\n". $this->page ."\r\n";
    }

    //获取删除按钮
    public function getDelBtn() {
        return $this->delBtn;
    }

    //获取表头
    public function getTableTitle() {
        $tableTitle = $this->tableTitle;
        $this->tableTitle = '';
        return $tableTitle;
    }

    //获取分页
    public function getPage() {
        $page = $this->page;
        $this->page = '';
        return $page;
    }
}